package com.qindian.coupontest;

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.widget.Toast;

/**
 * Author: Li
 * Date: 2018/4/28 0028
 * Description:
 */

public class BaseFragment extends Fragment {

    protected Context mContext;
    protected boolean mIsFirst = false;
    private ProgressDialog mDialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    protected void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getActivity() != null && isVisibleToUser && !mIsFirst) {
            mIsFirst = true;
            onFirstVisible();
        }
    }

    protected void onFirstVisible() {

    }

    protected void showLoadingDialog(String msg) {
        if (mDialog == null) {
            mDialog = new ProgressDialog(mContext);
        }
        mDialog.setMessage(msg);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }

    }

    protected void showLoadingDialog() {
        showLoadingDialog("加载中...");
    }

    protected void dismissLoadingDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        dismissLoadingDialog();

        super.onDestroyView();
    }
}
